/* 
 *   Date       Developer   Description
 *   
 *
 *   Classname:  zzz.test.component.mock.Test03
 * 
 *   Date:   2014-3-8
 * 
 *   The source code contained in this listing is proprietary to
 *   HLJ POST YITONG INFO-NET CO.,LTD.
 * 
 *   Unauthorized copying, adaptation, distribution, use, or display
 *   is strictly prohibited.
 *   This software is Copyright 2014 HLJ POST YITONG INFO-NET CO.,LTD.
 */
package com.titan.demo.camel.sample.component.mock;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.builder.xml.XPathBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;

import com.titan.demo.camel.utils.CamelUtils;


/**
 *
 *
 * @author Administrator
 *
 */
public class Test03 {

    public static void main(String[] args) {
        CamelContext context = null;
        ProducerTemplate template = null;
        try {
            context = new DefaultCamelContext();
            context.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("direct:a").to("mock:result");
                }
            });
            
            template = context.createProducerTemplate();
            template.start();
            context.start();
            
            MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class);
            
            mock.expectedMessageCount(2);
            sendMessages(template, 2);
            mock.assertIsSatisfied();

            mock.reset();
            mock.expectedMessageCount(3);
            sendMessages(template, 3);
            mock.assertIsSatisfied();

            mock.reset();
            mock.expectedMessageCount(10);
            sendMessages(template, 10);
            mock.assertIsSatisfied();
            
            mock.reset();
            mock.expectedHeaderReceived("header", "value");
            sendHeader(template, "header", "value");
            mock.assertIsSatisfied();
            
            mock.reset();
            mock.expectedHeaderReceived("header", "value1");
            sendHeader(template, "header", "value");
            mock.assertIsNotSatisfied();
            
            mock.reset();
            mock.expectedHeaderReceived("number", 123);
            String body = "<foo><id>123</id></foo>";
            XPathBuilder xpath = XPathBuilder.xpath("/foo/id", Integer.class);
            send(template, body, "number", xpath);
            mock.assertIsSatisfied();
            
            mock.reset();
            mock.expectedHeaderReceived("header", "value1");
            sendHeader(template, "header", "value");
            mock.assertIsNotSatisfied();
            
            mock.reset();
            mock.expectsAscending().body();
            mock.expectsAscending().header("counter");
            send(template, "1", "counter", 1);
            send(template, "2", "counter", 2);
            send(template, "3", "counter", 3);
            mock.assertIsSatisfied();
            
            mock.reset();
            mock.expectsDescending().body();
            mock.expectsDescending().header("counter");
            send(template, "3", "counter", 3);
            send(template, "2", "counter", 2);
            send(template, "1", "counter", 1);
            mock.assertIsSatisfied();
            
            mock.reset();
            mock.expectsNoDuplicates().body();
            mock.expectsNoDuplicates().header("counter");
            send(template, "3", "counter", 3);
            send(template, "2", "counter", 2);
            send(template, "1", "counter", 1);
            mock.assertIsSatisfied();
            
            mock.reset();
            mock.expectsNoDuplicates().body();
            mock.expectsNoDuplicates().header("counter");
            send(template, "3", "counter", 3);
            send(template, "2", "counter", 2);
            send(template, "2", "counter", 2);
            mock.assertIsNotSatisfied();            

            mock.reset();
            mock.expectedBodyReceived().constant("<message>1</message>");
            template.sendBody("direct:a", "<message>1</message>");
            mock.assertIsSatisfied();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CamelUtils.close(template);
            CamelUtils.close(context);
        }
    }
    
    
    private static void send(ProducerTemplate template, String body, String name, Object value) {
        template.sendBodyAndHeader("direct:a", body, name, value);
    }
    private static void sendMessages(ProducerTemplate template, int count) {
        for (int i=0; i<count; i++) {
            template.sendBody("direct:a", null);
        }
    }
    private static void sendHeader(ProducerTemplate template, String name, Object value) {
        send(template, null, name, value);
    }
}

